<?php

/**
 * iCMS - i Content Management System
 * Copyright (c) 2007-2017 iCMSdev.com. All rights reserved.
 *
 * @author icmsdev <master@icmsdev.com>
 * @site https://www.icmsdev.com
 * @licence https://www.icmsdev.com/LICENSE.html
 */
defined('iPHP') or exit('What are you doing?');
self::head();
?>
<script type="text/javascript">
    var typeMap = {
        'file': '文件',
        'dir': '目录'
    };
    //上传文件回调
    function modal_cb1(a) {}
    //选择文件或者目录回调
    function callback(a) {
        window.parent.modal_<?php echo $this->callback; ?>({
            "state": "SUCCESS",
            "data": {
                "value": a.value,
                "url": $(a).attr("url")
            }
        });
    }
    $(function() {
        var doc = $(document);
        var clickMode = "<?php echo $this->click; ?>";
        if (clickMode) {
            doc.on("change", "[data-type]", function(event) {
                event.preventDefault();
                var type = $(this).data('type');
                if ($(this).prop("checked")) {
                    if (type == clickMode) {
                        callback(this);
                    } else {
                        $(this).prop("checked", false);
                        if (type == "dir") {
                            var tr = $(this).closest('tr');
                            var href = $(".dirname", tr).attr('href');
                            console.log(href);
                            window.location.href = href;
                        }
                        iCMS.notify.info("当前模式只能选择" + typeMap['<?php echo $this->click; ?>']);
                    }
                }
            });
        }
        if (clickMode == 'file') {
            $('[data-toggle="checkAll"]').click(function() {
                var target = $(this).data('target');
                $('[data-type="file"]:checkbox', $(target)).each(function() {
                    if (this.checked) {
                        callback(this);
                    }
                });
            });
        }

        $('[i=mkdir]').click(function() {
            iCMS.ui.dialog({
                follow: this,
                height: 'auto',
                content: document.getElementById('mkdir-box'),
                modal: false,
                title: '创建新目录',
                okValue: '创建',
                ok: function() {
                    var a = $("#newdirname"),
                        n = a.val(),
                        d = this;
                    if (n == "") {
                        iCMS.ui.alert("请输入目录名称!");
                        a.focus();
                        return false;
                    } else {
                        var api = '<?php printf('%s=files&do=mkdir&CSRF_TOKEN=%s', ADMINCP_URL, Security::$CSRF_TOKEN); ?>';
                        $.post(api, {
                                name: n,
                                pwd: '<?php echo $pwd; ?>'
                            },
                            function(json) {
                                if (json.code) {
                                    d.content(json.message).button([{
                                        value: '完成',
                                        callback: function() {
                                            window.location.reload();
                                        },
                                        autofocus: true
                                    }]);
                                    window.setTimeout(function() {
                                        window.location.reload();
                                    }, 3000);
                                } else {
                                    iCMS.ui.alert(json.message);
                                    a.focus();
                                    return false;
                                }
                            }, "json");
                    }
                    return false;
                }
            });
        });
    });
</script>
<?php if (!AdmincpView::$IS_MODAL) { ?>
    <div class="content" id="<?php echo APP_MAINID; ?>">
    <?php } ?>
    <div class="block block-themed" id="files-explorer">
        <div class="block-header hide">
            <h3 class="block-title">文件管理</h3>
            <div class="block-options">
                <button type="button" class="btn-block-option" data-toggle="block-option" data-action="fullscreen_toggle"><i class="si si-size-fullscreen"></i></button>
                <button type="button" class="btn-block-option" data-toggle="block-option" data-action="content_toggle"><i class="si si-arrow-up"></i></button>
            </div>
            <div class="buttons">
                <a href="javascript:;" class="btn btn-mini btn-success">
                    <i class="fa fa-fw fa-folder"></i>
                    创建新目录
                </a>
                <a href="<?php echo ADMINCP_URL; ?>=files&do=multi&modal=true&dir=<?php echo $pwd; ?>" title="上传文件" data-toggle="modal" data-meta='{"width":"98%","height":"580px"}' class="btn btn-mini btn-primary" id="upload">
                    <i class="fa fa-fw fa-upload"></i>
                    上传文件
                </a>
            </div>
        </div>
        <ul class="nav nav-tabs nav-tabs-block align-items-center" data-toggle="tabs" role="tablist">
            <li class="nav-item">
                <a class="nav-link active" href="#">
                    <i class="fa fa-fw fa-file"></i>
                    文件管理
                </a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-btn dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">快捷链接</a>
                <div class="dropdown-menu">
                    <a class="dropdown-item" href="#" i="mkdir">
                        <i class="si si-plus text-success"></i>
                        新建目录
                    </a>
                    <div class="dropdown-divider"></div>
                    <a class="dropdown-item" href="<?php echo ADMINCP_URL; ?>=files&do=multi&modal=true&dir=<?php echo $pwd; ?>&callback=cb1" title="上传文件" data-toggle="modal" data-meta='{"width":"60%","height":"500px"}' class="btn btn-mini btn-primary" id="upload">
                        <i class="fa fa-fw fa-upload"></i>
                        上传文件
                    </a>
                </div>
            </li>
            <li class="nav-item">
                <a href="<?php echo $URI . $parent; ?>" class="form-control" title="当前路径">
                    iCMS://
                    <?php echo $pwd; ?>
                </a>
            </li>
            <li class="nav-item ml-auto">
                <div class="block-options pl-3 pr-2">
                    <button type="button" class="btn-block-option text-secondary" data-toggle="block-option" data-action="fullscreen_toggle"><i class="si si-size-fullscreen"></i></button>
                    <button type="button" class="btn-block-option text-secondary" data-toggle="block-option" data-action="content_toggle"><i class="si si-arrow-up"></i></button>
                </div>
            </li>
        </ul>
        <div class="block-content p-0">
            <form action="<?php echo APP_URL; ?>&do=batch" method="POST" id="<?php echo APP_FORMID; ?>" target="iPHP_FRAME">
                <table class="js-table-checkable table table-sm table-hover table-vcenter mb-0">
                    <thead class="thead-dark">
                        <tr>
                            <th style="width: 70px" class="align-middle"></th>
                            <th style="width: 320px">目录</th>
                            <th class="text-right pr-2">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td></td>
                            <td colspan="2">
                                <a href="<?php echo $URI . $parent; ?>">
                                    <i class="fa fa-fw fa-angle-double-up"></i>
                                    返回上级
                                </a>
                            </td>
                        </tr>
                        <?php if ($dirRs) {
                            $_count = count($dirRs);
                            for ($i = 0; $i < $_count; $i++) {
                        ?>
                                <tr id="<?php echo md5($dirRs[$i]['path']); ?>">
                                    <td class="text-center">
                                        <div class="custom-control custom-checkbox">
                                            <input data-type="dir" type="checkbox" class="custom-control-input" id="dir-<?php echo $i; ?>" value="<?php echo $dirRs[$i]['path']; ?>" />
                                            <label class="custom-control-label" for="dir-<?php echo $i; ?>"></label>
                                        </div>
                                    </td>
                                    <td>
                                        <a href="<?php echo $dirRs[$i]['url']; ?>" class="dirname"><?php echo $dirRs[$i]['name']; ?></a>
                                    </td>
                                    <td class="text-right">
                                        <div class="btn-group btn-group-sm">
                                            <?php if (0) { ?>
                                                <a class="btn btn-sm btn-light mv_dir" title="重命名"><i class="fa fa-fw fa-i-cursor"></i></a>
                                                <a class="btn btn-sm btn-light" title="上传文件" href="<?php echo ADMINCP_URL; ?>=files&do=multi&modal=true&dir=<?php echo $dirRs[$i]['path']; ?>" data-toggle="modal" data-meta='{"width":"98%","height":"580px"}' title="上传到此目录">
                                                    <i class="fa fa-fw fa-upload"></i>
                                                </a>
                                            <?php } ?>
                                            <a class="btn btn-sm btn-light" href="<?php echo ADMINCP_URL; ?>=files&do=deldir&path=<?php echo $dirRs[$i]['path']; ?>" target="iPHP_FRAME" title="删除目录" data-action="delete">
                                                <i class="fa fa-fw fa-trash"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>

                        <?php }
                        }  ?>
                    </tbody>
                </table>
                <?php if ($fileRs) { ?>
                    <table class="js-table-checkable table table-sm table-hover table-vcenter mb-0">
                        <thead class="thead-dark">
                            <tr>
                                <th style="width: 70px" class="text-center">
                                    <div class="custom-control custom-checkbox">
                                        <input type="checkbox" class="custom-control-input" id="check-all" data-toggle="checkAll" data-target="#filesList" />
                                        <label class="custom-control-label" for="check-all"></label>
                                    </div>
                                </th>
                                <th>文件名</th>
                                <th style="width: 60px">类型</th>
                                <th style="width: 90px">大小</th>
                                <th style="width: 150px">最后修改时间</th>
                                <th class="text-right pr-2">操作</th>
                            </tr>
                        </thead>
                        <tbody id="filesList">
                            <?php if ($parent) { ?>
                                <tr>
                                    <td></td>
                                    <td colspan="7">
                                        <a href="<?php echo $URI . $parent; ?>">
                                            <i class="fa fa-fw fa-angle-double-up"></i>
                                            返回上级
                                        </a>
                                    </td>
                                </tr>
                            <?php }  ?>
                            <?php
                            $_count    = count($fileRs);
                            for ($i = 0; $i < $_count; $i++) {
                                // $icon  = Files::icon($fileRs[$i]['name']);
                            ?>
                                <tr id="<?php echo md5($fileRs[$i]['path']); ?>">
                                    <td class="text-center">
                                        <div class="custom-control custom-checkbox">
                                            <input data-type="file" type="checkbox" class="custom-control-input" id="file-<?php echo $i; ?>" value="<?php echo $fileRs[$i]['path']; ?>" url="<?php echo $fileRs[$i]['url']; ?>" />
                                            <label class="custom-control-label" for="file-<?php echo $i; ?>"></label>
                                        </div>
                                    </td>
                                    <td>
                                        <?php if (in_array(strtolower($fileRs[$i]['ext']), FilesPic::$EXTS)) { ?>
                                            <a href="###" class="tip-right" title="<img src='<?php echo $fileRs[$i]['url']; ?>' width='120px'/>">
                                                <?php echo $icon; ?>
                                                <?php echo $fileRs[$i]['name']; ?>
                                            </a>
                                        <?php } else { ?>
                                            <?php echo $icon; ?>
                                            <?php echo $fileRs[$i]['name']; ?>
                                        <?php } ?>
                                    </td>
                                    <td>
                                        <span class="badge badge-success"><?php echo $fileRs[$i]['ext']; ?></span>
                                    </td>
                                    <td><?php echo $fileRs[$i]['size']; ?></td>
                                    <td><?php echo $fileRs[$i]['modified']; ?></td>
                                    <td class="text-right">
                                        <div class="btn-group btn-group-sm">
                                            <?php if (0) { ?>
                                                <a class="btn btn-sm btn-light" title="编辑"><i class="fa fa-fw fa-pencil-alt"></i></a>
                                                <a class="btn btn-sm btn-light" title="重命名"><i class="fa fa-fw fa-i-cursor"></i></a>
                                                <a class="btn btn-sm btn-light" href="<?php echo $href; ?>" data-toggle="modal" title="上传并覆盖文件" title="上传"><i class="fa fa-fw fa-upload"></i></a>
                                            <?php } ?>
                                            <?php if ($href) { ?>
                                                <a class="btn btn-sm btn-light" href="<?php echo $href; ?>" data-toggle="modal" title="查看文件">
                                                    <i class="fa fa-fw fa-eye"></i>
                                                    查看
                                                </a>
                                            <?php } ?>
                                            <a class="btn btn-sm btn-light" href="<?php echo ADMINCP_URL; ?>=files&do=delfile&path=<?php echo $fileRs[$i]['path']; ?>" target="iPHP_FRAME" title="删除文件" data-action="delete">
                                                <i class="fa fa-fw fa-trash"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            <?php }  ?>
                        </tbody>
                    </table>
                <?php }  ?>
            </form>
        </div>
    </div>
    <?php if (!AdmincpView::$IS_MODAL) { ?>
    </div>
<?php } ?>
<div id="mkdir-box" style="width: 150px; display: none">
    <input class="form-control" id="newdirname" type="text" placeholder="请输入目录名称" />
</div>
<?php self::foot(); ?>